#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <ctype.h>
#include <stdbool.h> /* C99 only */

void selection_sort(int[], int);
void program91();

#define SIZE 10

void generate_random_walk(char walk[SIZE][SIZE]) {
    int dictionary[4][2] = {{-1, 0}, {0, -1}, {1, 0}, {0, 1}};

    int x = 0;
    int y = 0;

    for (int i = 0; i < SIZE; ++i) {
        for (int j = 0; j < SIZE; ++j) {
            walk[i][j] = '.';
        }
    }
    char letter = 'A';
    walk[x][y] = letter++;

    while (letter <= 'Z') {
        int shuffled[] = {0, 1, 2, 3};
        for (int i = 3; i >= 0; i--) {
            int j = rand() % (i + 1); // 打乱算法
            int temp = shuffled[i];
            shuffled[i] = shuffled[j];
            shuffled[j] = temp;
        }

        int moved = 0;
        for (int i = 0; i < 4; ++i) {
            int cur_x = x + dictionary[shuffled[i]][0];
            int cur_y = y + dictionary[shuffled[i]][1];

            if(cur_x >= 0 && cur_x < SIZE
            && cur_y >= 0 && cur_y < SIZE
            && walk[cur_x][cur_y] == '.') {
                x = cur_x;
                y = cur_y;
                walk[x][y] = letter++;
                moved = 1;
                break;
            }
        }

        if (!moved) {
            return;
        }
    }
}

void print_array(char walk[SIZE][SIZE]) {
    for (int i = 0; i < SIZE; ++i) {
        for (int j = 0; j < SIZE; ++j) {
            printf("%c ", walk[i][j]);
        }
        printf("\n");
    }
}

// 变位词检测函数
void program92() {
    // 26字母计数器数组（索引0=a, 1=b,...25=z）
    int letter_counts[26] = {0};
    char ch;  // 临时存储输入字符
    
    // 读取第一个单词
    printf("\nEnter first word: ");
    while ((ch = getchar()) != '\n' && ch != EOF) {
        // 只处理字母字符，忽略大小写
        if (isalpha(ch)) {
            letter_counts[tolower(ch) - 'a']++; // 字母转小写后增加对应计数器
        }
    }

    // 读取第二个单词
    printf("Enter second word: ");
    while ((ch = getchar()) != '\n' && ch != EOF) {
        if (isalpha(ch)) {
            letter_counts[tolower(ch) - 'a']--; // 减少对应计数器
        }
    }

    // 验证计数器归零
    int is_anagram = 1; // 假设是变位词
    for (int i = 0; i < 26; i++) {
        if (letter_counts[i] != 0) {
            is_anagram = 0; // 发现非零计数器则否定
            break;          // 提前终止检测
        }
    }
    // 输出最终检测结果
    printf("The words are %s.\n", is_anagram ? "anagrams" : "not anagrams");
}

void check_anagram() {
    int letter_counts[26] = {0};

    char ch;
    printf("Enter first word: ");
    while((ch = getchar()) != '\n' && ch != EOF) {
        if (isalpha(ch)) {
            letter_counts[ch - 'a']++;
        }
    }

    printf("Enter second word: ");
    while ((ch = getchar()) != '\n' && ch != EOF) {
        if(isalpha(ch)) {
            letter_counts[ch - 'a']--;
        }
    }

    int is_anagram = 1;
    for (int i = 0; i < 26; ++i) {
        if(letter_counts[i] != 0) {
            is_anagram = 0;
        }
    }

    printf("The words are %s.\n", is_anagram ? "anagrams" : "not anagrams");
}

void loop_check_anagrams() {
    while (1) {
        check_anagram();
        printf("\n");
    }
}

void reverse_sentence() {
    char sentence[100];
    int length = 0;

    printf("Enter a sentence: ");
    char ch;
    char endCh;
    int i = 0;

    while ((ch = getchar()) != '\n' && ch != EOF){
        if (ch == '.' || ch == '?' || ch == '!' ) {
            endCh = ch;
        } else {
            sentence[i++] = ch;
        }
    }

    int end_word = i - 1;
    printf("Reversal of sentence:");
    for (; i >= 0; --i) {
        if(sentence[i] == ' ') {
            for (int j = i + 1; j <= end_word; j++) {
                printf("%c", sentence[j]);
            }
            end_word = i - 1;
            printf(" ");
        } else if(i == 0) {
            for (int j = i; j <= end_word; j++) {
                printf("%c", sentence[j]);
            }
        }
    }
    printf("%c\n", endCh);
}

void julius_caesar() {
    char sentence[100];
    int shift_amount;

    char ch;
    int length = 0;
    printf("Enter message to be encrypted: ");
    while ((ch = getchar()) != '\n' && ch != EOF) {
        sentence[length++] = ch;
    }

    printf("Enter shift amount (1-25): ");
    scanf("%d", &shift_amount);

    printf("Encrypted message: ");
    for (int i = 0; i < length; ++i) {
        char ch = sentence[i];
        if(ch != ' ' && ch != '.' && ch != ',') {
            if(ch >= 'a' && ch <= 'z') {
                ch = ((ch - 'a') + shift_amount) % 26 + 'a';
            }
            if(ch >= 'A' && ch <= 'Z') {
                ch = ((ch - 'A') + shift_amount) % 26 + 'A';
            }
        }
        printf("%c", ch);
    }

    printf("\n");
}

int roll_dice() {
    int i, j;
    i = rand() % 6 + 1;
    j = rand() % 6 + 1;
    return i + j;
}

bool play_game() {
    int target = roll_dice();
    printf("You rolled: %d\n", target);

    if (target == 7 || target == 11) {
        return true;
    } else if(target == 2 || target == 3 || target == 12) {
        return false;
    }

    printf("You point is %d\n", target);
    int cur;
    for(;;) {
        cur = roll_dice();
        printf("You rolled: %d\n", cur);
        if(cur == 7) {
            return false;
        } else if(cur == target) {
            return true;
        }
    }
}
// 掷骰子游戏
/*
 第一次掷骰子 7/11 获胜 
            2/3/12 ->落败
            
记录第一次掷骰子和为“目标”
 
 后续在次掷出“目标”点数，赢
 掷出 7 落败
 其他情况，游戏继续
 */
void dice_game() {
    char keep;
    bool is_win;
    int win = 0;
    int loss = 0;
    for(;;) {
        is_win = play_game();
        if(is_win) {
            win++;
            printf("You win!\n");
        } else {
            loss++;
            printf("You lose!\n");
        }
        printf("Play again? ");
        scanf("%c", &keep);
        if (keep == 'n') break;
    }
    printf("Wins: %d  Losses: %d\n", win, loss);
}

int main(void) {
    srand(time(NULL));
    dice_game();
//    julius_caesar();
//    reverse_sentence();
//    loop_check_anagrams();
//    srand(time(NULL));
//    char walk[10][10];
//    generate_random_walk(walk);
//    print_array(walk);
//    program91();

    return 0;
}

void program91() {
    int n;
    printf("input arr length:");
    scanf("%d", &n);
    printf("input arr element:");
    int arr[n];

    for (int i = 0; i < n; ++i) {
        scanf("%d", &arr[i]);
    }

    selection_sort(arr, n);
    for (int i = 0; i < n; ++i) {
        printf("%d ", arr[i]);
    }
    printf("\n");
}

void selection_sort(int arr[], int n) {
    if (n < 2) return;
    int max_index = 0;
    for (int i = 1; i < n; ++i) {
        if(arr[i] > arr[max_index]) {
            max_index = i;
        }
    }

    int temp = arr[max_index];
    arr[max_index] = arr[n - 1];
    arr[n - 1] = temp;
    selection_sort(arr, n - 1);
}

